<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Twitter Settings Controller
 *
 * PHP version 5
 * LICENSE: This source file is subject to AGPL license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/licenses/agpl.html
 * @author	   Omar Vega Ramos <ovruni@gnu.org.pe> 
 * @copyright      (c) 2013 - 2016 Omar Vega Ramos
 * @license	   http://www.gnu.org/licenses/agpl.html GNU Affero General Public License (AGPL) 
 */

class Twitter_Settings_Controller extends Admin_Controller {
  
  public function index()
  {
    $this->template->this_page = 'addons';
    
    // Standard Settings View
    $this->template->content = new View("admin/addons/plugin_settings");
    $this->template->content->title = "Twitter Settings";
    
    // Settings Form View
    $this->template->content->settings_form = new View("twitter/admin/twitter_settings");
    
    // setup and initialize form field names
    $form = array(
		  'twitter_consumer_key' => '',
		  'twitter_consumer_secret' => '',
		  'twitter_access_token' => '',
		  'twitter_access_token_secret' => '',
		  );
    
    //  Copy the form as errors, so the errors will be stored with keys
    //  corresponding to the form field names
    $errors = $form;
    $form_error = FALSE;
    $form_saved = FALSE;
    $twitter_connect = '';
    $twitter_reset = '';
    
    // check, has the form been submitted, if so, setup validation
    if ($_POST) {
      // Instantiate Validation, use $post, so we don't overwrite $_POST
      // fields with our own things
      $post = new Validation($_POST);
      
      // Add some filters
      $post->pre_filter('trim', TRUE);
      
      // Add some rules, the input field, followed by a list of checks, carried out in order
      $post->add_rules('twitter_consumer_key', 'required', 'length[1,30]');
      $post->add_rules('twitter_consumer_secret', 'required', 'length[1,100]');
      $post->add_rules('twitter_access_token', 'required', 'length[1,100]');
      $post->add_rules('twitter_access_token_secret', 'required', 'length[1,100]');
      
      // Test to see if things passed the rule checks
      if ($post->validate()) {
	// Yes! everything is valid
	$twitter = ORM::factory('twitter', 1);
	$twitter->consumer_key = $post->twitter_consumer_key;
	$twitter->consumer_secret = $post->twitter_consumer_secret;
	$twitter->oauth_token = $post->twitter_access_token;
	$twitter->oauth_token_secret = $post->twitter_access_token_secret;
	$twitter->save();
	
	// Everything is A-Okay!
	$form_saved = TRUE;
	
	// repopulate the form fields
	$form = arr::overwrite($form, $post->as_array());
	
      // No! We have validation errors, we need to show the form again,
      // with the errors
      } else {
	// repopulate the form fields
	$form = arr::overwrite($form, $post->as_array());
	
	// populate the error fields, if any
	$errors = arr::overwrite($errors, $post->errors('settings'));
	$form_error = TRUE;
      }
    } else {
      // Retrieve Current Settings
      $twitter = ORM::factory('twitter', 1);
      
      $form = array(
		    'twitter_consumer_key' => $twitter->consumer_key,
		    'twitter_consumer_secret' => $twitter->consumer_secret,
		    'twitter_access_token' =>  $twitter->oauth_token,
		    'twitter_access_token_secret' =>  $twitter->oauth_token_secret,
		    );
    }
    
    // Pass the $form on to the settings_form variable in the view
    $this->template->content->settings_form->form = $form;
    
    // Other variables
    $this->template->content->errors = $errors;
    $this->template->content->form_error = $form_error;
    $this->template->content->form_saved = $form_saved;
  }
  
  /**
   * Reset Twitter Settings
   */
  public function reset()
  {
    $this->template = '';
    $this->auto_render = FALSE;
	
    // Retrieve Current Settings
    $twitter = ORM::factory('twitter', 1);
    $twitter->consumer_key = NULL;
    $twitter->consumer_secret = NULL;
    $twitter->oauth_token = NULL;
    $twitter->oauth_token_secret = NULL;
    $twitter->save();
    url::redirect('admin/twitter_settings');
  }
}
